<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User_model extends CI_Model {
    
    public $user_tbl 		= 'users';
	public $userweb_tbl 	= 'user_websites';
	public $role_tbl		= 'roles';
	public $log_tbl			= 'user_logs';
	public $menu_tbl		= 'menus';
	
	public function __construct()
    {
        parent::__construct();
    }
    
    // ------------------------------------------------------------------------
    
    public function count_users()
    {
        $query = $this->db->select('user_id')
                          ->where('deleted', 'n')
                          ->get($this->user_tbl);
                          
        return $query->num_rows();
    }
    
    // ------------------------------------------------------------------------
    
    public function list_users($limit = '', $offset = '', $order = 'created desc', $where = '')
    {
        $offset = $offset > 0 ? ($offset - 1) * $limit : 0;
        
        $query = $this->db->select()
						  ->join($this->role_tbl . ' AS r', 'r.role_id = u.role_id', 'left')
						  ->where('u.deleted', 'n')
                          ->order_by($order)
                          ->get($this->user_tbl . ' AS u', $limit, $offset);
                          
        return $query->num_rows() > 0 ? $query->result_array() : FALSE;
    }
    
    // ------------------------------------------------------------------------
	
	public function get_user($value = '', $where = 'u.user_id')
	{
		$query = $this->db->select('u.*, r.*, GROUP_CONCAT(uw.site_code) AS user_websites')
						  ->join($this->role_tbl . ' AS r', 'r.role_id = u.role_id', 'left')
						  ->join($this->userweb_tbl . ' AS uw', 'uw.user_id = u.user_id', 'left')
						  ->where($where, $value)
						  ->where('u.deleted', 'n')
						  ->get($this->user_tbl . ' AS u', 1);
						  
		return $query->num_rows() > 0 ? $query->row_array() : FALSE;
	}
	
	// ------------------------------------------------------------------------
    
    public function delete_user($user_id = '')
    {
        $query = $this->db->where('user_id', $user_id)
                          ->where('deleted', 'n')
                          ->update($this->user_tbl, array('deleted' => 'y'));
                          
        return TRUE;
    }
    
    // ------------------------------------------------------------------------
	
	public function update_user($fields, $user_id)
	{
		$query = $this->db->where('user_id', $user_id)
						  ->where('deleted', 'n')
						  ->update($this->user_tbl, $fields);
						  
		return TRUE;
	}
	
	// ------------------------------------------------------------------------
	
	public function log_action($fields)
	{
		$fields['created'] = date('Y-m-d H:i:s');
		
		$this->db->insert('user_logs', $fields);
		return TRUE;
	}
	
	// ------------------------------------------------------------------------
	
	public function list_logs($limit = '', $offset = '', $order = 'created desc')
	{
		$offset = $offset > 0 ? ($offset - 1) * $limit : 0;
		
		$query = $this->db->select('l.*, u.firstname, u.lastname')
						  ->join($this->user_tbl . ' AS u', 'u.user_id = l.user_id', 'left')
						  ->order_by($order)
						  ->get($this->log_tbl . ' AS l', $limit, $offset);
						  
		return $query->num_rows() > 0 ? $query->result_array() : FALSE;
	}
	
	// ------------------------------------------------------------------------
    
	public function count_logs()
	{
		$query = $this->db->select('user_log_id')
						  ->get($this->log_tbl);
						  
		return $query->num_rows();
	}
	
	// ------------------------------------------------------------------------
	
	
}

/* End of file user_model.php */
/* Location: ./application/models/user_model.php */